package com.Arsanesia.TempleRush.Models;

import com.Arsanesia.TempleRush.Controllers.World;
import com.Arsanesia.TempleRush.Controllers.Listeners.GeenieClickListener;
import com.Arsanesia.TempleRush.Enumeration.GeenieEnumeration;
import com.Arsanesia.TempleRush.Enumeration.GeenieStateEnumeration;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.badlogic.gdx.utils.Array;

public class Geenie implements Updateable{
	public static final int MOVE_RIGHT = 1;
	public static final int MOVE_LEFT = -1;
	public static final float DEFAULT_SPEED = 1f;
	public final String DESTROYED_GEENIE = "DESTROYED";
	private final float ANIMATION_TIME = 0.25f;
	private final float DESTROYEDANIMATION_TIME = ANIMATION_TIME - 0.15f;

	
	private GeenieEnumeration geenieType;
	private GeenieStateEnumeration geenieState;
	private Animation liveAnimation;
	private Animation destroyedAnimation;
	
	public TextureRegion currentFrame;
	private Image geenieImage;
	private float x,y;

	public TextureRegion test;
	
	private boolean isActive;
	private boolean isDestroyed;
	private float stateTime;
	private int direction;
	private float speed;
	private float width;
	private float height;
	private Rectangle bound;
	
	public Geenie(){
		isActive = false;
		isDestroyed = false;
		stateTime = 0f;
		//set geenie as fly
		geenieState = GeenieStateEnumeration.FLY;
		geenieImage = new Image();
		setX(-1000); setY(-1000);
		resetGeenieSize();
		bound = new Rectangle();
		
	}
	public Geenie(TextureRegion tr, World world){
		isActive = false;
		isDestroyed = false;
		stateTime = 0f;
		//set geenie as fly
		geenieState = GeenieStateEnumeration.FLY;
		geenieImage = new Image(tr);
		//set width and height
		width = tr.getRegionWidth();
		height = tr.getRegionHeight();
		bound = new Rectangle(x, y, width, height);
		//geenieImage.setBounds(geenieImage.getX(), geenieImage.getY(), width+20, height+20);
		direction = MOVE_LEFT;
		//add click listener
		geenieImage.addListener(new GeenieClickListener(this,world));
		setX(-1000); setY(-1000);
		resetGeenieSize();

	}

	public Geenie(GeenieEnumeration g_enum, int direction, float speed, float x, float y){
		this();
		this.direction = direction;
		this.speed = speed;
		this.x = x; this.y = y;
		geenieImage.setPosition(x, y);
		bound.set(x, y, width, height);
		Gdx.app.log("geenie", "bound: "+x+","+y+","+width+","+height);
	} 
	//setter
	public void setLiveAnimation(Array<Sprite> thesprites){
		this.liveAnimation = new Animation(ANIMATION_TIME, new Array<TextureRegion>(thesprites));
		//geenieImage = new Image(new TextureRegionDrawable(liveAnimation.getKeyFrame(stateTime)));
		stateTime = 0;
	}
	
	public void setDestroyedAnimation(Array<Sprite> thesprites){
		this.destroyedAnimation = new Animation(DESTROYEDANIMATION_TIME, new Array<TextureRegion>(thesprites));
	}
	public void setGeenieType(GeenieEnumeration ge){
		this.geenieType = ge;
	}
	public void setActive(boolean _active){
		this.isActive = _active;
		stateTime = 0f;
	}
	public void setAttributes(GeenieEnumeration color, int new_direction, 
								float _speed, float x_pos, float y_pos){
		geenieType = color;
		this.direction = new_direction;	
		
		
		this.speed = _speed;	

		//update the image position
		this.x = x_pos;
		this.y = y_pos;
		geenieImage.setX(x_pos);
		geenieImage.setY(y_pos);
		bound.set(x, y, width, height);

	}
	public void setX(float x){
		this.x = x;
		geenieImage.setX(x);
		bound.setX(x);
	}
	public void setY(float y){
		this.y = y;
		geenieImage.setY(y);
		bound.setY(y);
	}
	public void setStateTime(float time){
		this.stateTime = time;
	}
	public void setDestroyed(boolean destroy){
		this.isDestroyed = destroy;
		stateTime = 0;
	}
	public void setGeenieState(GeenieStateEnumeration gs){
		this.geenieState = gs;
	}
	
	//getter
	public GeenieEnumeration getGeenieType(){
		return this.geenieType;
	}
	public boolean isActive(){
		return this.isActive;
	}
	public int getDirection(){
		return this.direction;
	}
	public GeenieStateEnumeration getState(){
		return this.geenieState;
	}
	public Image getGeenieImage(){
		return this.geenieImage;
	}
	public float getX(){
		return this.x;
	}
	public float getY(){
		return this.y;
	}
	public Animation getLiveAnimation(){
		return this.liveAnimation;
	}
	public Animation getDestroyedAnimation(){
		return this.destroyedAnimation;
	}
	public boolean isDestroyed(){
		return this.isDestroyed;
	}
	public float getWidth(){
		return this.width;
	}
	public float getHeight(){
		return this.height;
		
	}
	public Rectangle getBound(){
		return this.bound;
	}
	
	public void resetGeenieSize(){
		this.width = this.geenieImage.getImageWidth();
		this.height = this.geenieImage.getImageHeight();
		bound.set(x, y, width, height);
	}
	@Override
	public void update(){
		//get the animation time
		stateTime+= Gdx.graphics.getDeltaTime();
		
		//move geenie if not destroyed
		if(!isDestroyed){
			if(this.geenieState == GeenieStateEnumeration.FLY) {
				this.setX(geenieImage.getX() + (direction * (speed)));
			}
			else{
				setGeenieState(GeenieStateEnumeration.FLY);
			}
		}
		

		
		//update the animation image
		currentFrame = new TextureRegion((!isDestroyed)?(liveAnimation.getKeyFrame(stateTime, true)):
											(destroyedAnimation.getKeyFrame(stateTime, false)));
		//flip the image
		if(this.direction == MOVE_RIGHT&& currentFrame.getRegionWidth()>0 || this.direction==MOVE_LEFT && currentFrame.getRegionWidth()<0) 
			currentFrame.flip(true, false);

		geenieImage.setDrawable(new TextureRegionDrawable(currentFrame));

		//state as destroyed after destroyedanimation finished
		if(isDestroyed && destroyedAnimation.isAnimationFinished(stateTime)){ 
			isActive = false;
			isDestroyed = false;
			setGeenieState(GeenieStateEnumeration.DISAPPEAR);
		}
		
	}
	public boolean isOverlaps(Rectangle bound2) {
		return bound.overlaps(bound2);
	}
	
}
